using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Peradigm.Framework.UI.Web;

using Liberty.Application.CaseManagement;
using Liberty.Application.GrantManagement;
using Liberty.Application.ServiceMemberManagement;
using Liberty.Domain.CaseManagement;
using Liberty.Domain.GrantManagement;
using Liberty.Domain.NoteManagement;
using Liberty.Domain.ServiceMemberManagement;

namespace Liberty.UI.Service.ServiceMemberManagement
{
    public partial class ServiceMemberView : AbstractPage
    {
        #region Constants
        private const string 
            CASE_VIEW_RELATIVE_URL = "~/CaseManagement/CaseView.aspx",
            GRANT_VIEW_RELATIVE_URL = "~/GrantManagement/GrantView.aspx",
            ADD_CASE_RELATIVE_URL = "~/GrantApplicationWizardView.aspx?case=true",
            ADD_GRANT_RELATIVE_URL = "~/GrantApplicationWizardView.aspx?grant=true",
            PAGE_GRANT_LIST_VIEW = "../GrantManagement/GrantListView.aspx",
            SERVICE_MEMBER_URL = "ServiceMemberView.aspx";
        #endregion

        #region Event Handlers

        //protected override void OnPreRender( EventArgs e )
        //{
        //    this.ucNoteListView.Model = this.NotesFrom( this.ServiceMember );
        //}

        protected void Page_Load( object sender, EventArgs e )
        {  
            List<Note> notes = this.NotesFrom( this.ServiceMember );
            List<Case> cases = this.CasesFrom( this.ServiceMember );
            List<Grant> grants = this.GrantsFrom( this.ServiceMember );

            this.ucServiceMemberDetailView.Model = this.ServiceMember;
            this.ucServiceMemberEditView.Model = this.ServiceMember;
            this.ucNoteListView.Model = notes;
            this.ucCaseSummaryView.Model = cases;
            this.ucGrantSummaryView.Model = grants;
            this.ucGrantSummaryView.GrantSelectedListeners += new GrantSelectedEventHandler( ucGrantSummaryView_GrantSelectedListeners );
            
            this.ucNewNoteView.NoteAddedListeners += new NoteAdded( this.OnNoteAdded );
            this.ucCaseSummaryView.CaseSelectedListeners += new CaseSelectedEventHandler( this.ucCaseSummaryView_CaseSelectedListeners );
            this.ucCaseSummaryView.AddNewCaseListeners += new AddNewCaseEventHandler( ucCaseSummaryView_AddNewCaseListeners );
            this.ucCaseSummaryView.AddNewGrantListeners += new AddNewGrantEventHandler( ucCaseSummaryView_AddNewGrantListeners );

            


            this.Page.Title = this.ServiceMember.Rank + " " + this.ServiceMember.CanonicalName;
            this.ucGrantSummaryView.AddGrantButton.Visible = false;

        }
        private void ucGrantSummaryView_GrantSelectedListeners( Grant grant )
        {
            GrantVariable grantVariable = new GrantVariable();
            grantVariable.Grant = grant;
            Server.Transfer( GRANT_VIEW_RELATIVE_URL );
        }
        void ucCaseSummaryView_AddNewGrantListeners( Case aCase )
        {
            CaseVariable caseVariable = new CaseVariable();
            caseVariable.Case = aCase;
            this.Response.Redirect( ADD_GRANT_RELATIVE_URL );
        }

        void ucCaseSummaryView_AddNewCaseListeners(ServiceMember serviceMember)
        {
            ServiceMemberVariable serviceMemberVariable = new ServiceMemberVariable();
            serviceMemberVariable.ServiceMember = serviceMember;            
            this.Response.Redirect( ADD_CASE_RELATIVE_URL );
        }

        private void ucCaseSummaryView_CaseSelectedListeners( Case aCase )
        {
            CaseVariable caseVariable = new CaseVariable();
            caseVariable.Case = aCase;
            this.Response.Redirect( CASE_VIEW_RELATIVE_URL );
        }

        protected void cmdEdit_Click( object sender, EventArgs e )
        {
            this.EditServiceMember();
        }

        protected void cmdSave_Click( object sender, EventArgs e )
        {
            this.ucServiceMemberEditView.SaveModel();

#warning display readonly or if adding new grant move to next page
            //throw new NotImplementedException( "OnServiceMembderEdited Not Implemented" );
            Response.Redirect( SERVICE_MEMBER_URL );
        }

        protected void cmdCancel_Click( object sender, EventArgs e )
        {
            Response.Redirect( SERVICE_MEMBER_URL );
        }
        #endregion

        #region Methods
        protected override void UpdateView()
        {
            base.UpdateView();
            ( (Main)this.Master ).HideAddGrantAction();
            ( (Main)this.Master ).HideServiceMembersAction();
        }
        #endregion

        #region Properties
        public override object Model
        {
            get
            {
                ServiceMemberVariable variable = new ServiceMemberVariable();
                return variable.ServiceMember;
            }
            set
            {
                ServiceMemberVariable variable = new ServiceMemberVariable();
                variable.ServiceMember = (ServiceMember)value;
            }
        }
        #endregion

        #region Private Methods
        private List<Note> NotesFrom( ServiceMember serviceMember )
        {
        ServiceMemberController controller = new ServiceMemberController();
            return controller.NotesFrom( serviceMember );
        }

        private List<Grant> GrantsFrom( ServiceMember serviceMember )
        {
            ServiceMemberController controller = new ServiceMemberController();
            return controller.GrantsFrom( serviceMember );
        }

        private List<Case> CasesFrom( ServiceMember serviceMember )
        {
            ServiceMemberController controller = new ServiceMemberController();
            return controller.CasesFrom( serviceMember );
        }

        private void EditServiceMember()
        {
            this.cmdEdit.Visible = false;
            this.cmdSave.Visible = true;
            this.cmdCancel.Visible = true;
            this.CasesHeader.Visible = false;
            this.GrantsHeader.Visible = false;
            this.CasesPanel.Enabled = false;
            this.GrantsPanel.Enabled = false;
            this.ucServiceMemberDetailView.Visible = false;
            this.ucServiceMemberEditView.Visible = true;
            this.ucNewNoteView.Visible = false;
            this.ucNoteListView.Visible = false;
            this.ucCaseSummaryView.Visible = false;
            this.ucGrantSummaryView.Visible = false;
        }

        protected void OnNoteAdded( Note note )
        {
            ServiceMemberController controller = new ServiceMemberController();
            controller.SaveNote( this.ServiceMember, note );
            this.ucNoteListView.Model = this.NotesFrom( this.ServiceMember );
        }
        #endregion

        #region Private Properties
        private ServiceMember ServiceMember
        {
            get
            {
                return (ServiceMember)this.Model;
            }
        }
        #endregion

        #region Construction and Finalization

        #endregion

        #region Data Elements
        #endregion
  
}
}
